/**
  ******************************************************************************
  * @author     Chris
  * @since      2024/7/1 20:44
  *
  * @file       hd_ina226.c
  * @brief      Xxx hardware driver.
  *
  * @note       This file contains the hardware driver for the Xxx.
  *
  * @warning    None.
  ******************************************************************************
  * Change Logs:
  *   Date          Author       Notes
  *   2024/7/1     Chris        the first version
  *
  ******************************************************************************
  */

#include "hd_ina226.h"


static void INA226_init_(const INA226 *this) {
    uint16_t tmp = 0x2741;
    this->vptr->write_data16(this, INA226_CONFIG, &tmp);
    tmp = 0x000A;
    this->vptr->write_data16(this, INA226_CALIBRATION, &tmp);
}

static uint8_t INA226_readShuntVoltage_(const INA226 *this, uint16_t *shuntVoltage) {
    return this->vptr->read_data16(this, INA226_SHUNT_VOLTAGE, shuntVoltage);
}

static uint8_t INA226_readBusVoltage_(const INA226 *this, uint16_t *busVoltage) {
    return this->vptr->read_data16(this, INA226_BUS_VOLTAGE, busVoltage);
}

static uint8_t INA226_readPower_(const INA226 *this, uint16_t *power) {
    return this->vptr->read_data16(this, INA226_POWER, power);
}

static uint8_t INA226_readCurrent_(const INA226 *this, uint16_t *current) {
    return this->vptr->read_data16(this, INA226_CURRENT, current);
}

static uint8_t INA226_readManuID_(const INA226 *this, uint16_t *id) {
    return this->vptr->read_data16(this, INA226_MANU_ID, id);
}

static uint8_t INA226_readDieID_(const INA226 *this, uint16_t *id) {
    return this->vptr->read_data16(this, INA226_DIE_ID, id);
}

static uint8_t INA226_readData16_(const INA226 *this, uint8_t regAddr, uint16_t *regVal) {
    return I2C_read(this->i2c, INA226_SLAVE_ADDR, regAddr, (uint8_t *)regVal, 2);
}

static uint8_t INA226_writeData16_(const INA226 *this, uint8_t regAddr, uint16_t *regVal) {
    return I2C_send(this->i2c, INA226_SLAVE_ADDR, regAddr, (uint8_t *)regVal, 2);
}

void INA226_constructor(INA226 *this, I2C *i2c) {
    static struct INA226Vtbl vtbl = {
        .init         = &INA226_init_,
        .read_shunt   = &INA226_readShuntVoltage_,
        .read_bus     = &INA226_readBusVoltage_,
        .read_power   = &INA226_readPower_,
        .read_current = &INA226_readCurrent_,
        .read_manu_id = &INA226_readManuID_,
        .read_die_id  = &INA226_readDieID_,
        .write_data16 = &INA226_writeData16_,
        .read_data16  = &INA226_readData16_,
    };

    this->vptr = &vtbl;
    this->i2c = i2c;
}
